package com.bt.springboot.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zkx
 * @Date 2024/6/18 17:51
 */
@Configuration
public class DelayedExchangeConfig {

	public static final String DELAYED_EXCHANGE_NAME = "delayed_exchange";
	public static final String DELAYED_QUEUE_NAME = "delayed_queue";

	public static final String DELAYED_ROUTING_KEY = "delayed_routingKey";

	@Bean
	public CustomExchange delayedExchange() {
		Map<String, Object> args = new HashMap<>();
		//自定义交换机的类型，指定分发方式
		args.put("x-delayed-type", "direct");
		return new CustomExchange(DELAYED_EXCHANGE_NAME,
				// 消息类型
				"x-delayed-message",
				// 是否持久化
				true,
				// 是否自动删除
				false,args);
	}

	@Bean
	public Queue delayedQueue() {
		return QueueBuilder.durable(DELAYED_QUEUE_NAME).withArgument("x-delayed-type", "direct").build();
	}

	@Bean
	public Binding delayedQueueBinding() {
		return BindingBuilder.bind(delayedQueue()).to(delayedExchange()).with(DELAYED_ROUTING_KEY).noargs();
	}

}
